package com.cskaoyan.javase.object._6clone._2exercise;
/**
 * >  现在有三个类 FirstLevel 、SecondLevel 、ThirdLevel
 *
 * FirstLevel 类有三个属性：
 * int firstIntValue;
 * double firstDoubleValue;
 * SecondLevel second;
 *
 * SecondLevel 类有三个属性：
 * int secondIntValue;
 * double secondDoubleValue;
 * ThirdLevel third;
 *
 * ThirdLevel 类有两个属性：
 * int thirdIntValue;
 * double thirdDoubleValue;
 *
 * 请完成FirstLevel的深度克隆
 *
 *
 * String这个类是比较特殊的,由于修改它的字符串内容,原先的对象内容不变,所以无需对String做深度克隆
 * String特殊性在于String对象不可变
 * 一旦想要修改某个String对象的字符串内容,就会创建一个新的对象,而不是在原先对象的基础上修改(它想修改也改不了)
 *
 *
 * @since 17:26
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Demo {
    public static void main(String[] args) throws CloneNotSupportedException {
        FirstLevel first = new FirstLevel(10, 20, new SecondLevel(100, 200, new ThirdLevel(1000, 2000,"张三")));

        FirstLevel cloneFirst = first.clone();
        System.out.println(first);
        System.out.println(cloneFirst);
        System.out.println("----------------------------");
        System.out.println(first.second);
        System.out.println(cloneFirst.second);
        System.out.println("----------------------------");
        System.out.println(first.second.third);
        System.out.println(cloneFirst.second.third);
        System.out.println("----------------------------");
        first.second.third.name = "李四";
        System.out.println(first.second.third.name);
        System.out.println(cloneFirst.second.third.name);

    }
}
class FirstLevel implements Cloneable{
    int firstIntValue;
    double firstDoubleValue;
    SecondLevel second;

    public FirstLevel() {
    }

    public FirstLevel(int firstIntValue, double firstDoubleValue, SecondLevel second) {
        this.firstIntValue = firstIntValue;
        this.firstDoubleValue = firstDoubleValue;
        this.second = second;
    }

    // 做第一层深度克隆
    @Override
    protected FirstLevel clone() throws CloneNotSupportedException {
        // 1.深克隆是在浅克隆的基础上完成的
        FirstLevel cloneFirst = (FirstLevel) super.clone();
        // 2.克隆一份second对象
        SecondLevel cloneSecond = this.second.clone();
        // 3.将克隆后的FirstLevel对象的拷贝second引用指向拷贝second对象
        cloneFirst.second = cloneSecond;
        return cloneFirst;
    }
}

class SecondLevel implements Cloneable{
    int secondIntValue;
    double secondDoubleValue;
    ThirdLevel third;

    public SecondLevel() {
    }

    public SecondLevel(int secondIntValue, double secondDoubleValue, ThirdLevel third) {
        this.secondIntValue = secondIntValue;
        this.secondDoubleValue = secondDoubleValue;
        this.third = third;
    }

    // 做第二层深度克隆
    @Override
    protected SecondLevel clone() throws CloneNotSupportedException {
        // 1.深克隆是在浅克隆的基础上完成的
        SecondLevel cloneSecond = (SecondLevel) super.clone();
        // 2.克隆一份third对象
        ThirdLevel cloneThird = this.third.clone();
        // 3.将克隆后的SecondLevel对象的拷贝third引用指向拷贝third对象
        cloneSecond.third = cloneThird;
        return cloneSecond;
    }
}
class ThirdLevel implements Cloneable{
    int thirdIntValue;
    double thirdDoubleValue;
    String name;

    public ThirdLevel() {
    }

    public ThirdLevel(int thirdIntValue, double thirdDoubleValue, String name) {
        this.thirdIntValue = thirdIntValue;
        this.thirdDoubleValue = thirdDoubleValue;
        this.name = name;
    }

    @Override
    protected ThirdLevel clone() throws CloneNotSupportedException {
        return (ThirdLevel) super.clone();
    }
}